<?php
/*
 * @Description: 短信类
 * @Author: QianLong
 * @Date: 2019-09-27 16:34:02
 * @LastEditors    : QianLong
 * @LastEditTime   : 2021-04-17 11:02:12
 */

namespace app\common\service;

use think\facade\Db;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

class Email
{
    /**
     * 邮件发送类
     * @param string $subject 邮件标题
     * @param string $htmlBody HTML邮件内容
     * @param array $recipient 收件人：单地址=666666@qq.com、带用户名单地址=['email'=>'6666@qq.com','name'=>'qianlong']、多用户=[['email'=>'6666@qq.com','name'=>'qianlong'],['email'=>'9999@qq.com','name'=>'qianlong']]
     * @param string $textBody 如果邮件客户端不支持HTML则显示此内容
     * @param array $attachment 附件地址：单附件=/data/test.zip、多附件=['/data/test.zip','/data/file.txt']
     * @param array $cc 超送：单地址=5555@qq.com、多地址=['6666@qq.com','8888@qq.com']
     * @return void
     * @author QianLong <87498106@qq.com>
     * @date 2021-04-17 10:01:25
     * @editAuthor QianLong <87498106@qq.com>
     * @editDescription 
     * @editDate 2021-04-17 10:01:25
     */
    public static function send($subject, $htmlBody, $recipient = null, string $textBody = '', $attachment = null, $cc = null)
    {
        $infoData = Db::name('SysSmtpSetting')->where('id', 1)->find();
        //Instantiation and passing `true` enables exceptions
        $mail = new PHPMailer(true);
        try {
            $mail->CharSet = 'UTF-8';
            //Server settings
            $mail->SMTPDebug = SMTP::DEBUG_SERVER;//Enable verbose debug output
            $mail->isSMTP();//Send using SMTP
            $mail->Host       = $infoData['host'];//Set the SMTP server to send through
            $mail->SMTPAuth   = true;//Enable SMTP authentication
            $mail->Username   = $infoData['username'];//SMTP username
            $mail->Password   = $infoData['password'];//SMTP password
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;//Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
            $mail->Port       = $infoData['port'];//TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
            //Recipients
            $mail->setFrom($infoData['from'], $infoData['from_name']);
            if (!is_array($recipient)) {
                $mail->addAddress($recipient);
            } else {
                if (isset($recipient['email'])) {
                    $mail->addAddress($recipient['email'], $recipient['name']);
                } else {
                    foreach ($recipient as $key => $vo) {
                        $mail->addAddress($vo['email'], (isset($vo['name']))?$vo['name']:'');
                    }
                }
            }
            if (!empty($infoData['reply'])) {
                $mail->addReplyTo($infoData['reply']);
            }
            if (!empty($cc)) {
                if (!is_array($cc)) {
                    $mail->addCC($cc);
                } else {
                    foreach ($cc as $vo) {
                        $mail->addCC($vo);
                    }
                }
            }
            if (!empty($attachment)) {
                if (!is_array($attachment)) {
                    $mail->addAttachment($attachment);
                } else {
                    foreach ($attachment as $file) {
                        $mail->addAttachment($file);
                    }
                }
            }
            // $mail->addBCC('bcc@example.com');
            //Content
            $mail->isHTML(true);                                  //Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body    = $htmlBody;
            if (!empty($textBody)) {
                $mail->AltBody = $textBody;
            }
            $mail->send();
            return ['code' => 200];
        } catch (Exception $e) {
            return ['code' => -1, 'msg' => $mail->ErrorInfo];
        }
    }
}
